<?php


namespace app\home\controller;


use app\admin\model\CheckInModel;
use app\admin\model\QuanGuanMemberModel;
use app\home\services\MemberService;
use app\utils\Response;
use think\Db;
use think\validate\ValidateRule;


class CheckInController extends ApiBase
{
    public function check()
    {
        $startTime = strtotime(date("Y-m-d"), time());
        $endTime = strtotime(date("Y-m-d"), time());
        if (!isset($this->requestData['num']) || (int)$this->requestData['num'] <= 0) {
            Response::fail("击打次数异常");
        }
        $memberService = new MemberService();
        $checkInModel = new CheckInModel();
        $memberInfo = $memberService->getMemberInfoByToken($this->memberInfo['token']);
        // 是否已经签到
        $checkInfo = $checkInModel->where([
            'create_id' => $memberInfo['id'],
        ])->whereBetweenTime('check_date', $startTime, $endTime)->find();
        if ($checkInfo['is_check'] != 1) {
            // 未签到
            $checkInfo->check_date = date("Y-m-d");
            $checkInfo->create_id = $memberInfo['id'];
            $checkInfo->check_time = time();
            $checkInfo->is_check = 1;
            $checkInfo->num = $this->requestData['num'];
            $checkInfo->save();
        } else {
            // 修改击打次数
            $checkInfo->num = $this->requestData['num'];
            $checkInfo->save();
        }
        Response::success();
    }

    /**
     * 插叙连续签到次数
     */
    public function getCheckDays()
    {
        $memberService = new MemberService();
        $memberInfo = $memberService->getMemberInfoByToken($this->memberInfo['token'], ['id']);
        $sql = "
            SELECT
                create_id,
                DATEDIFF( DATE_FORMAT(NOW(),'%Y%m%d') , fdate_max ) AS fconsecutive_days
            FROM
                ( SELECT create_id, max( check_date ) as fdate_max FROM qg_check_in WHERE is_check = 0 and create_id = {$memberInfo['id']}  ) t1; ";
        $res = Db::query($sql);
        if ($res[0]['fconsecutive_days'] < 0) {
            $res[0]['fconsecutive_days'] = 0;
        }
        Response::success([
            'max_days' => $res[0]['fconsecutive_days']
        ]);
    }

    // 只需要在上线的时候执行一次
    public function initCheckDate()
    {
        $type = (int)input('type', 0);
        if ($type <= 0) {
            die('类型异常');
        }
        $memberModel = new QuanGuanMemberModel();
        $studentList = $memberModel->where([
            'status' => 1,
            'is_delete' => 1
        ])->whereIn('identity', '1,2')->field('id')->select();
        if ($type == 1) {
            $date = date('Y-m-d', strtotime('-1 day'));

        } else {
            $date = date('Y-m-d', time());
        }
        foreach ($studentList as $student) {
            $checkInModel = new CheckInModel();
            $checkInModel->create_id = $student['id'];
            $checkInModel->create_time = 0;
            $checkInModel->check_date = $date;
            $checkInModel->save();
        }
    }

    /**
     * 获取学生的打卡记录列表
     */
    public function getCheckList()
    {
        $checkInModel = new CheckInModel();
        $list = $checkInModel->where([
            'create_id' => $this->memberInfo['id'],
            'is_delete' => 1,
            'status' => 1,
            'is_check' => 1
        ])->field(['id', 'num', 'check_time'])->select();
        foreach ($list as &$item) {
            $item['check_time'] = date("Y-m-d H:i:s", $item['check_time']);
        }
        Response::success([
            'check_list' => $list
        ]);
    }
}